package team.seekers.hula.user.userFollow.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import team.seekers.hula.auth.service.MindUtils;
import team.seekers.hula.user.user.entity.dao.UserDO;
import team.seekers.hula.user.userFollow.entity.dao.UserFollowerDO;
import team.seekers.hula.user.userFollow.entity.vo.UserFocusListVO;
import team.seekers.hula.user.userFollow.mapper.UserFollowerMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import team.seekers.hula.user.user.service.UserService;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户关注一个用户 服务实现类
 * </p>
 *
 * @author hbc
 * @since 2021-02-18
 */
@Service
public class UserFollowerService extends ServiceImpl<UserFollowerMapper, UserFollowerDO> {

    @Autowired
    private UserService userService;

    public List<String> listAllFocusId(String userId) {
        QueryWrapper<UserFollowerDO> wrapper = new QueryWrapper<>();
        wrapper.eq("follower_id",userId);
        List<UserFollowerDO> list = list(wrapper);
        if(list == null){
            return null;
        }
        List<String> allFocusIdList = new ArrayList<>();
        for (UserFollowerDO userFollowerDO : list) {
            allFocusIdList.add(userFollowerDO.getUserId());
        }
        return allFocusIdList;
    }

    public List<UserFocusListVO> listAllFocus() {
        QueryWrapper<UserFollowerDO> wrapper = new QueryWrapper<>();
        wrapper.eq("follower_id", MindUtils.getUserId());
        List<UserFollowerDO> list = list(wrapper);
        if(list == null){
            return new ArrayList<>();
        }
        List<UserFocusListVO> allFocusList = new ArrayList<>();
        for (UserFollowerDO userFollowerDO : list) {
            UserDO userDO = userService.getById(userFollowerDO.getUserId());
            if (userDO == null){
                return new ArrayList<>();
            }
            UserFocusListVO userFocusListVO = new UserFocusListVO();
            BeanUtils.copyProperties(userDO, userFocusListVO);
            allFocusList.add(userFocusListVO);
        }
        return allFocusList;
    }
}
